package com.sap.ondeabastecer;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class TabMapActivity extends MapActivity{

	private MapView mapView;
	private MapController mapController;
	private LocationManager locationManager;
	private LocationListener locationListener;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.maptabview);
		setMapController();
		setLocationListener();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setMapController() {
    	
    	try {
			mapView = (MapView) findViewById(R.id.mapview);
			mapController = mapView.getController();
			mapController.setZoom(17);
		} 
    	catch (Exception e) {
			System.out.println(">>> OndeAbastecerActivity.setMapController() - " + e.getMessage());
			e.printStackTrace();
		}
    }
	
	private void setLocationListener() {
    	try {
			// Acquire a reference to the system Location Manager
			locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
			
			//Define a listener that responds to the location updates
			locationListener = new LocationListener() {
				
				@Override
				public void onStatusChanged(String provider, int status, Bundle extras) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProviderEnabled(String provider) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProviderDisabled(String provider) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onLocationChanged(Location location) {
					processLocation(location);
					
				}
			};
			
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, locationListener);
		} 
    	catch (Exception e) {
    		System.out.println(">>> OndeAbastecerActivity.setLocationListener() - " + e.getMessage());
			e.printStackTrace();
		}
    }
    
    public void processLocation(Location location) {
    	
    	
    	mapController.animateTo(new GeoPoint( (int)(location.getLatitude() * 1000000), (int)(location.getLongitude() * 1000000) ) );
    	
    	//Toast.makeText(this, location.getLatitude() + "-" + location.getLongitude(), Toast.LENGTH_SHORT).show();
    }

}
